package pers.cz.jefconfig.reader.xml;

import org.w3c.dom.*;
import org.xml.sax.SAXException;
import pers.cz.jefconfig.tools.JefStringBuilder;
import pers.cz.jefconfig.tools.XmlUtils;
import pers.cz.jefconfig.reader.Reader;

import javax.xml.parsers.ParserConfigurationException;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

/**
 * @program: JefConfig
 * @description: 解析xml
 * @author: Cheng Zhi
 * @create: 2023-04-16 15:28
 **/
public class XmlReader implements Reader {

    /**
     * 保存根节点，后续保存到map，默认去掉根节点。
     */
    private String rootName;

    @Override
    public void readLine(FileInputStream fileInputStream, Map<String, Object> map) throws IOException {

        try {
            Document document = XmlUtils.loadDocument(fileInputStream, null, true);
            Element documentElement = document.getDocumentElement();
            rootName = documentElement.getNodeName();
            readElements(documentElement, map);
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取键值对
     * @param parent
     * @param map
     */
    private void readElements(Node parent, Map<String, Object> map) {
        NodeList nl = parent.getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            int type = node.getNodeType();

            if (type == Node.TEXT_NODE) {

                String value = "";
                String key = "";
                String nodeValue = node.getNodeValue();

                if (nodeValue != null && !nodeValue.trim().isEmpty()) {
                    JefStringBuilder keyStringBuilder = new JefStringBuilder();
                    value = nodeValue;
                    Node nodeBak = node;
                    while (true) {
                        Node parentNode = nodeBak.getParentNode();
                        if (parentNode != null && parentNode.getNodeName() != null) {
                            keyStringBuilder.preAppand("." + parentNode.getNodeName());
                        } else {
                            break;
                        }
                        nodeBak = parentNode;
                    }
                    if (!keyStringBuilder.toString().trim().isEmpty() && !value.trim().isEmpty()) {
                        key = keyStringBuilder.toString().trim();
                        // .#document.root.db.url:jdbc:mysql://127.0.0.1:3306/postgirl  默认去除.#document.root.
                        key = key.substring(11+rootName.length() + 1, key.length());
                        map.put(key, value);
                    }
                }


            }
            if (type == Node.ATTRIBUTE_NODE || type == Node.CDATA_SECTION_NODE || type == Node.TEXT_NODE) {
                continue;
            }
            // 如果不是注释则递归
            if (type != Node.COMMENT_NODE) {
                readElements(node, map);
            } else {
                processComment((Comment) node);
            }
        }
    }

    /**
     * 读取注释
     * @param node
     */
    private void processComment(Comment node) {
        String text=node.getNodeValue();
    }
}


